package com.mediacloud.app.newsmodule.widget.flow;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mediacloud.app.newsmodule.R;

import org.jetbrains.annotations.NotNull;


/**
 * @author kgf
 * @describe 折叠
 * @date on 2022/8/23
 */
public class SearchHistoryLayout extends FlowListView {
    private View upFoldView;
    private View downFoldView;
    private boolean isCheck;

    public void setCheck(boolean check) {
        isCheck = check;
    }

    public SearchHistoryLayout(Context context) {
        this(context, null);
    }

    public SearchHistoryLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchHistoryLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        upFoldView = LayoutInflater.from(context).inflate(R.layout.view_item_fold_up, null);
        downFoldView = LayoutInflater.from(context).inflate(R.layout.view_item_fold_down, null);
        upFoldView.setOnClickListener(v -> {
            if(!isCheck){
                return;
            }
            mFold = false;
            flowAdapter.notifyDataChanged();
        });

        downFoldView.setOnClickListener(v -> {
            if(!isCheck){
                return;
            }
            mFold = true;
            flowAdapter.notifyDataChanged();
        });

        setOnFoldChangedListener((canFold, fold, index, surplusWidth) -> {
            if (canFold) {
                removeFromParent(downFoldView);
                addView(downFoldView);
                if (fold) {
                    removeFromParent(upFoldView);
                    int upIndex = index(index, surplusWidth);
                    addView(upFoldView, upIndex);
                } else {
                    removeFromParent(downFoldView);
                    addView(downFoldView);
                }
            }
        });
    }
    public void hideView(){
        upFoldView.setVisibility(View.GONE);
        downFoldView.setVisibility(View.GONE);
    }

    public void showView(){
        upFoldView.setVisibility(View.VISIBLE);
        downFoldView.setVisibility(View.VISIBLE);
    }

    /**
     * 移除父布局中的子布局
     *
     * @param view
     */
    public void removeFromParent(@NotNull View view) {
        if (view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
    }
    public static  int getViewWidth(View view) {
        view.measure(0, 0);
        return view.getMeasuredWidth();
    }
    private int index(int index, int surplusWidth) {
        int upIndex = index;
        int upWidth = getViewWidth(upFoldView);
        //当剩余空间大于等于展开View宽度直接加入index+1
        if (surplusWidth >= upWidth) {
            upIndex = index + 1;
        } else { //找到对应的位置
            for (int i = index; i >= 0; i--) {
                View view = getChildAt(index);
                int viewWidth = getViewWidth(view);
                upWidth -= viewWidth;
                if (upWidth <= 0) {
                    upIndex = i;
                    break;
                }
            }
        }
        return upIndex;
    }


}
